package com.lanou.myapplication.sportsFragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.myapplication.R;
import com.lanou.myapplication.entertainmentAdapter.EntertainmentrecyclerAdapter;
import com.lanou.myapplication.entertainmentAdapter.ViewPagerAdapter2;
import com.lanou.myapplication.entity.Entertainment;
import com.lanou.myapplication.entity.Sports;
import com.lanou.myapplication.sportsAdapter.SportsrecyclerAdapter;
import com.lanou.myapplication.sportsAdapter.ViewPagerAdapter3;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/23.
 */
public class Sportsfragment extends Fragment{
    private ViewPager pager;//广告栏部分
    private ViewPagerAdapter3 pagerAdapter;//广告栏适配器对象
    private RequestQueue queue;//定义一个请求队列

    private RecyclerView recyclerView;
    //定义内容列表的适配器对象
    private SportsrecyclerAdapter sportsrecyclerAdapter;

    private Handler handler; //定义一个handler对象,实现广告轮播过程
    private int currentItem = 0;
    private int maxItem = 0;



    private Runnable r = new Runnable() {
        @Override
        public void run() {
            currentItem++;
            maxItem = pagerAdapter.getCount();
            pager.setCurrentItem(currentItem % maxItem);

            handler.postDelayed(r, 4000);
        }
    };



    @Nullable

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.activity_news_sportspage, null);
        initView(v);
        initData();
        return v;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    private void initView(View view) {
        pager = (ViewPager)view.findViewById(R.id.sptopviewPager);
        //构建适配器对象
        pagerAdapter = new ViewPagerAdapter3(getActivity().getSupportFragmentManager());
        pager.setAdapter(pagerAdapter);//绑定适配器


       recyclerView=(RecyclerView)view.findViewById(R.id.sprecyclerView);
        GridLayoutManager gridLayoutManager =new GridLayoutManager(getActivity(),1);
        recyclerView.setLayoutManager(gridLayoutManager);

        //初始化适配器对象
        sportsrecyclerAdapter =new SportsrecyclerAdapter(getActivity());
        recyclerView.setAdapter(sportsrecyclerAdapter);
        //初始化handler对象
        handler = new Handler();
    }

    private void initData() {
        //初始化请求队列
        queue = Volley.newRequestQueue(getContext());

        //获取最新接口消息
        String url = "http://c.m.163.com/nc/article/list/T1348649079062/0-20.html";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                //按对象解析返回结果
                Sports tl= gson.fromJson(response, Sports.class);
                if (tl != null) {
                    List<Sports.T1348649079062Entity> data = tl.getT1348649079062();
                    if (data != null && data.size() > 0) {
                        pagerAdapter.addData(data);

                    //    maxItem=4;
                        maxItem=data.size();
                        handler.postDelayed(r, 4000);
                        sportsrecyclerAdapter.addData(data);
                    }
                }
            }
        },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };

        queue.add(request);//将请求加入到请求队列中

    }


    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (queue != null)
            queue.cancelAll(this);
    }
}